package com.coocaa.ops.admin.v1.audit.provider.impl;

import com.coocaa.ops.admin.base.AbstractBizImpl;
import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.v1.audit.model.CheckInfoModel;
import com.coocaa.ops.admin.v1.audit.provider.AbstractAuditTaskProviderImpl;
import com.coocaa.ops.admin.v1.panel.biz.impl.PanelInfoBizImpl;
import com.coocaa.ops.admin.v1.panel.service.impl.PanelInfoQueryServiceImpl;
import com.coocaa.ops.common.core.base.model.ResourceBaseModel;
import com.coocaa.ops.common.core.base.util.ResourceStatusValidator;
import com.coocaa.ops.common.core.biz.cornericon.entity.GlobalCornerIconEntity;
import com.coocaa.ops.common.core.biz.panel.constant.BlockTypeConstants;
import com.coocaa.ops.common.core.biz.panel.constant.PanelTypeConstants;
import com.coocaa.ops.common.core.biz.panel.entity.BlockContentConfigInfo;
import com.coocaa.ops.common.core.biz.panel.entity.PanelInfoEntity;
import com.coocaa.ops.common.core.biz.panel.entity.SubPanelConfigInfo;
import com.coocaa.ops.common.core.biz.panel.entity.VirtuallyBlockInfo;
import com.coocaa.ops.common.core.biz.poster.entity.GlobalPictureEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author bijiahao
 * @date : 2018/6/15.
 * @description: 审核前校验-审核-撤销审核-上架操作 提供者接口实现-板块
 */
@Service
public class PanelAuditTaskProviderImpl extends AbstractAuditTaskProviderImpl<PanelInfoEntity> {

    @Autowired
    private PanelInfoBizImpl panelInfoBiz;
    @Autowired
    private PanelInfoQueryServiceImpl panelInfoQueryService;
    private static CommonLanguageProperties commonLanguageProperties;

    @Override
    public AbstractBizImpl getAbstractBizImpl() {
        return panelInfoBiz;
    }

    /**
     * 审核前校验操作
     *
     * @param resourceBaseModel
     * @return
     */
    @Override
    public CheckInfoModel checkBeforeAudit(ResourceBaseModel resourceBaseModel) {
        PanelInfoEntity entity = panelInfoQueryService.getDetailInfo(resourceBaseModel.getId(), resourceBaseModel.getVersion());
        if (PanelTypeConstants.isMark(entity.getPanelType())) {
            return CheckInfoModel.getCheckInfoModel(Boolean.FALSE, null);
        }
        if (PanelTypeConstants.isGroup(entity.getPanelType())) {
            List<SubPanelConfigInfo> subPanelConfigInfoList = entity.getSubPanelList();
            for (SubPanelConfigInfo subPanelConfigInfo : subPanelConfigInfoList) {
                List<VirtuallyBlockInfo> virtuallyBlockInfoList = subPanelConfigInfo.getBlockList();
                CheckInfoModel checkInfoModel = this.validVirtuallyBlockInfoList(virtuallyBlockInfoList);
                if (checkInfoModel.isResult()) {
                    return checkInfoModel;
                }
            }
        }
        if (PanelTypeConstants.isNormal(entity.getPanelType())) {
            List<VirtuallyBlockInfo> virtuallyBlockInfoList = entity.getBlockList();
            CheckInfoModel checkInfoModel = this.validVirtuallyBlockInfoList(virtuallyBlockInfoList);
            if (checkInfoModel.isResult()) {
                return checkInfoModel;
            }
        }
        return CheckInfoModel.getCheckInfoModel(Boolean.FALSE, null);
    }


    /*************** ===私有方法=== ********************* ===start=== ***************************************/

    private CheckInfoModel validVirtuallyBlockInfoList(List<VirtuallyBlockInfo> virtuallyBlockInfoList) {
        CheckInfoModel checkInfoModel;
        for (VirtuallyBlockInfo virtuallyBlockInfo : virtuallyBlockInfoList) {
            List<BlockContentConfigInfo> commonBlockList = virtuallyBlockInfo.getCommonBlockList();
            checkInfoModel = this.validBlockContentList(commonBlockList);
            if (checkInfoModel.isResult()) {
                return checkInfoModel;
            }
            List<BlockContentConfigInfo> dmpBlockList = virtuallyBlockInfo.getDmpBlockList();
            checkInfoModel = this.validBlockContentList(dmpBlockList);
            if (checkInfoModel.isResult()) {
                return checkInfoModel;
            }
        }
        return CheckInfoModel.getCheckInfoModel(Boolean.FALSE, null);
    }

    private CheckInfoModel validBlockContentList(List<BlockContentConfigInfo> blockList) {
        if (CollectionUtils.isEmpty(blockList)) {
            return CheckInfoModel.getCheckInfoModel(Boolean.FALSE, null);
        }
        for (BlockContentConfigInfo blockContentConfigInfo : blockList) {
            if (!BlockTypeConstants.isCheckPoster(blockContentConfigInfo.getBlockType())) {
                continue;
            }
            GlobalPictureEntity globalPicture = blockContentConfigInfo.getGlobalPicture();
            Assert.notNull(globalPicture, "poster info "+commonLanguageProperties.getNotNull());
            if (ResourceStatusValidator.isNotOnline(globalPicture.getStatus())) {
                return CheckInfoModel.getCheckInfoModel(Boolean.TRUE, getCheckMsg(blockContentConfigInfo));
            }
            CheckInfoModel checkCornerIcon = this.checkCornerIcon(blockContentConfigInfo);
            if (checkCornerIcon.isResult()) {
                return checkCornerIcon;
            }
        }
        return CheckInfoModel.getCheckInfoModel(Boolean.FALSE, null);
    }


    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        PanelAuditTaskProviderImpl.commonLanguageProperties = commonLanguageProperties;
    }

    /**
     * 校验角标状态
     *
     * @param blockContentConfigInfo
     * @return
     */
    private CheckInfoModel checkCornerIcon(BlockContentConfigInfo blockContentConfigInfo) {
        if (!BlockTypeConstants.isCheckPoster(blockContentConfigInfo.getBlockType())) {
            return CheckInfoModel.getCheckInfoModel(Boolean.FALSE, null);
        }
        List<GlobalCornerIconEntity> cornerList = blockContentConfigInfo.getCornerList();
        if (CollectionUtils.isEmpty(cornerList)) {
            return CheckInfoModel.getCheckInfoModel(Boolean.FALSE, null);

        }
        for (GlobalCornerIconEntity cornerIconEntity : cornerList) {
            if (null != cornerIconEntity && ResourceStatusValidator.isNotOnline(cornerIconEntity.getStatus())) {
                return CheckInfoModel.getCheckInfoModel(Boolean.TRUE, getCheckMsgForCornerIcon(blockContentConfigInfo, cornerIconEntity));

            }
        }
        return CheckInfoModel.getCheckInfoModel(Boolean.FALSE, null);
    }


    /**
     * 获取校验信息
     *
     * @param blockContentConfigInfo
     * @return
     */
    private String getCheckMsg(BlockContentConfigInfo blockContentConfigInfo) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("pictureID: [")
                .append(blockContentConfigInfo.getGlobalPicture().getId())
                .append("],")
                .append("blockID: [")
                .append(blockContentConfigInfo.getId())
                .append("],")
                .append("blockName： ")
                .append(blockContentConfigInfo.getTitle())
        ;
        return stringBuilder.toString();
    }

    /**
     * 获取校验信息
     *
     * @param blockContentConfigInfo
     * @return
     */
    private String getCheckMsgForCornerIcon(BlockContentConfigInfo blockContentConfigInfo, GlobalCornerIconEntity cornerIconEntity) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("iconID: [")
                .append(cornerIconEntity.getId())
                .append("],")
                .append("blockID: [")
                .append(blockContentConfigInfo.getId())
                .append("],")
                .append("blockName： ")
                .append(blockContentConfigInfo.getTitle())
        ;
        return stringBuilder.toString();
    }
}
